<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :edit="edit" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr,
		errorModal
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				newProductCache:'mar-yxhd-customerComplaint-title2-newProduct-cache',
				unitCache:'mar-yxhd-customerComplaint-title2-unit-cache'
			})
		},
		methods:{
			clearNewProductCode(){
				let _that = this
				_that.$refs.autoform.clear("title1",'newProductId')
				_that.$refs.autoform.clear("title1",'newProductName')
				_that.$refs.autoform.clear("title1",'newProductStd')
				_that.$refs.autoform.clear("title1",'customerProductName')
				_that.$refs.autoform.clear("title1",'customerProductStd')
				_that.$refs.autoform.clear("title1",'unitId')
				_that.$refs.autoform.clear("title1",'orderNum')
				_that.$refs.autoform.clear("title1",'defectiveProductsNumber')
				_that.$refs.autoform.clear("title1",'defectRatio')
				_that.$refs.autoform.clear("title1",'invoiceCode')
				_that.$refs.autoform.clear("title1",'defectDescription')
				_that.$refs.autoform.changeEditor("title1","fileList","tempFileList",[])
			},
			onInputDefectiveProductsNumber(val){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(formdata.orderNum){
					if(Number(val) > Number(formdata.orderNum)){
						let title1 = _that.$refs.autoform.getEditorTitle("title1","orderNum")
						let title2 = _that.$refs.autoform.getEditorTitle("title1","defectiveProductsNumber")
						errorModal(_that,'提示信息',title2+'不能大于'+title1,()=>{
							_that.$refs.autoform.clear("title1","defectiveProductsNumber")
						})
					}else{
						let num = Number(val)/Number(formdata.orderNum) * 100
						formdata.defectRatio =  Number(num).toFixed(2)
					}
				}
			},
			onInputReturnedQuantity(val){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(formdata.orderNum){
					if(Number(val) > Number(formdata.orderNum)){
						let title1 = _that.$refs.autoform.getEditorTitle("title1","orderNum")
						let title2 = _that.$refs.autoform.getEditorTitle("title1","returnedQuantity")
						errorModal(_that,'提示信息',title2+'不能大于'+title1,()=>{
							_that.$refs.autoform.clear("title1","returnedQuantity")
						})
					}
				}
			},
			initload(){
				let _that = this
				let arr = [
					_that.om.request(
						"dictSource/loadCombo",
						{
							sourceId:'289',
							categoryId:'9009'
						}
					),
					_that.om.request(
						"dictSource/loadCombo",
						{
							sourceId:'289',
							categoryId:'9004'
						}
					)
				]
				Promise.all(arr).then(res=>{
					_that.$refs.autoform.changeEditor("title1","unqualifiedPhenomenonCategoryId","data",configSelectData(res[0],'id','text'))
					_that.$refs.autoform.initSelectIndex("title1","unqualifiedPhenomenonCategoryId")
					_that.$refs.autoform.changeEditor("title1","unqualifiedFindLocationId","data",configSelectData(res[1],'id','text'))
					_that.$refs.autoform.initSelectIndex("title1","unqualifiedFindLocationId")
				})
			},
			
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				if (e.configDataCacheKey) {
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if (res.data && res.data instanceof Array) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
							}
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							changeEditor("title1","newProductId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({}),		
									url:'productShowSecond/getProductPage',
									initload:true,
									searchfield:'q',
									namefield:'newProductName',
									notefield:'newProductStd',
									cachename:_that.newProductCache
								}
							},null,_that.autoformdata)
							changeEditor("title1","unitId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({}),		
									url:'uomGroup/selectAllChildGrid',
									initload:true,
									searchfield:'q',
									namefield:'unitName',
									extrafield:'unitCode',
									notefield:'unitGroupName',
									cachename:_that.unitCache
								}
							},null,_that.autoformdata)
							changeEditor(_that.autoformdata[0].editors, "fileList", "fileUploadKey", "81")
							if(_that.formdata.fileList) {
								var tempFileList = []
								var fileUrlList = _that.formdata.fileList.map(t=>{
									tempFileList.push(t.fileSaveName)
									return t.fileSaveName
								})
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileUrlList", fileUrlList)
								changeEditor(_that.autoformdata[0].editors, "fileList", "tempFileList", tempFileList)
							}
							
							_that.initload()
						}
					})
				}
			})
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key:_that.newProductCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.newProductId = d.id
							formdata.newProductCode = d.newProductCode
							formdata.newProductName = d.newProductName  
							formdata.newProductStd = d.newProductStd
							formdata.customerProductName = d.projectName
							formdata.unitId = d.unitId
							formdata.unitName = d.unitName
							uni.removeStorage({
								key:_that.newProductCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.unitCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.unitId = d.id
							formdata.unitName = d.unitName
							uni.removeStorage({
								key:_that.unitCache
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
